import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/BaseMaterialInfo.dart';
import 'package:xiongan_app_yy/pages/common/TextFieldItem.dart';
import 'package:xiongan_app_yy/pages/common/TextItemCard.dart';
import 'package:xiongan_app_yy/utils/image_utils.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

class InventoryOperationDetail extends StatefulWidget {
  final Map data;
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _InventoryOperationDetail();
  }

  InventoryOperationDetail(this.data);

}

class _InventoryOperationDetail extends State<InventoryOperationDetail> {
  String image="assets/images/build.png";
  TextEditingController controller0 = TextEditingController();
  TextEditingController controller1 = TextEditingController();
  bool flag=true;

  @override
  void initState() {
    super.initState();
    if(widget.data["materialUri"]!=null){
      ImageDownloadUtil.getImageBase64(widget.data["materialUri"], (data){
        setState(() {
          image=data;
        });
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "盘库作业",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Scrollbar(
              child: SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    TextItemCard(
                      name: "盘库托盘码",
                      value: widget.data["worktoolcode"]??"",
                    ),
                    TextItemCard(
                      name: "盘库库区",
                      value: widget.data["areaname"],
                    ),
                    TextItemCard(
                      name: "盘库货架",
                      value: widget.data["shelfname"],
                    ),
                    TextItemCard(
                      name: "盘库货位",
                      value: widget.data["locationcode"],
                    ),
                    Column(
                      children: <Widget>[
                        BaseMaterialInfo(
                          imgwid: ImageDownloadUtil.isImageType(image),
                          options: {"materialname":"物资名称","materialcode":"实物ID","baseunitname":"基本单位","totalcount":"数量"},
                          data: widget.data,
                        ),
                        Container(
                          padding: EdgeInsets.all(15),
                          child: Column(
                            children: <Widget>[
                              Container(
                                child: Row(
                                  children: <Widget>[
                                    Expanded(
                                      child: TextFieldItem(
                                        enabled: flag,
                                        textEditingController: controller0,
                                          hintText: "扫描或输入盘库清点位置"),
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(left: 10),
                                      child: FlatButton(
                                        color: Color(0xFF42AFF0),
                                        padding: EdgeInsets.only(
                                            top: 13,
                                            bottom: 13,
                                            left: 10,
                                            right: 10),
                                        child: Text(
                                          "发送指令下架",
                                          style: TextStyle(
                                              fontSize: 12,
                                              color: Colors.white),
                                        ),
                                        onPressed: () {
                                          print(flag);
                                          if(controller0.text.length>0){
                                            if(flag==true){
                                              downTask();
                                            }else{
                                              TostUtil.showToast("下架成功,已锁定");
                                            }
                                          }else{
                                            TostUtil.showToast("请输入盘库清点位置");
                                          }
                                        },
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              TextFieldItem(inputFormatters: [
                                WhitelistingTextInputFormatter(RegExp("[0-9]")),
                              ],textEditingController: controller1,hintText: "输入实际盘库数量")
                            ],
                          ),
                        )
                      ],
                    )
                  ],
                ),
              ),
            ),
          ),
          Row(
            children: <Widget>[
              Expanded(
                child: FlatButton(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(0))),
                  padding: EdgeInsets.all(15),
                  color: Color(0xFF42AFF0),
                  child: Text(
                    "提交并上架托盘",
                    style: TextStyle(color: Colors.white),
                  ),
                  onPressed: () {
                    if(controller1.text.length>0){
                      UpTask();
                    }else{
                      TostUtil.showToast("请输入实际盘库数量");
                    }
                  },
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  void downTask() {
    InternetAPI.getInstance().post("WMS/MiWarehouseCheck/SendWarehouseCheckDownTask",
            (data) {
          if(data==true){
            setState(() {
              flag=false;
              print(flag);
            });
            TostUtil.showToast("下架成功");
          }
        },errorCallBack: (err){

        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
          'checklocationcode': controller0.text,
          'checkdetailid': widget.data["checkdetailid"],

        });
  }

  void UpTask() {
    InternetAPI.getInstance().post("WMS/MiWarehouseCheck/SendWarehouseCheckUpTask",
            (data) {
          print(data);
          if (data == true) {
            Navigator.pop(context,"ok");
            TostUtil.showToast("提交成功");
          }
        },errorCallBack: (err){

        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'warehouseid': DataSingleton().warehouseChoice.id,
          'operaterUserName': DataSingleton().Data.username,
          'checklocationcode': controller0.text,
          'realquantity': controller1.text,
          'id': widget.data["id"],
          'checkdetailid': widget.data["checkdetailid"],

        });
  }
}
